import { createRouter, createWebHashHistory } from "vue-router";
import { useUserStore } from "@/stores";
// 引入i18n
import { createI18n, useI18n } from 'vue-i18n';
import zh from '@/language/locales/zh-cn.json';
import en from '@/language/locales/en-us.json';

// 初始化i18n实例（这通常在main.js中完成，此处仅为演示）
const i18nInstance = createI18n({
    locale: 'zh', // 默认语言
    messages: {
        zh: zh,
        en: en,
    },
});
// 使用i18n实例的t方法来获取翻译文本
function getTranslation(key) {
    return i18nInstance.global.t(key);
}
export const routes = [
    {
        path: "/",
        name: "index",
        components: {
            default: () => import("../views/login/index.vue"),
        },
        meta: {
            title: 'title.Login',
        },
    },
    // {
    //     path: "/dashboard",
    //     name: "dashboard",
    //     components: {
    //         default: () => import("../layout/index.vue"),
    //     },
    // },
    {
        path: '/layout',
        name: 'layout',
        component: () => import( /* webpackChunkName: "home" */ '../layout/index.vue'),
        meta: {
            showTitle: true,
            // title: 'title.Dashboard',
        },
        children: [
            {
                // 用户管理
                path: '/layout/userManagement',
                name: 'userManagement',
                component: () => import( /* webpackChunkName: "popup" */ '../views/user/index.vue'),
                meta: {
                    showTitle: true,
                    title: 'title.UserManagement',
                },
            },
            {
                // 套餐管理
                path: '/layout/packageManagement',
                name: 'packageManagement',
                component: () => import( /* webpackChunkName: "popup" */ '../views/package/index.vue'),
                meta: {
                    showTitle: true,
                    title: 'title.PackageManagement',
                },
            },
            {
                // 资产管理
                path: '/layout/assetManagement',
                name: 'assetManagement',
                component: () => import( /* webpackChunkName: "popup" */ '../views/assetManagement/index.vue'),
                meta: {
                    showTitle: true,
                    title: 'title.ReceivingAddress',
                },
            },
            {
                // 币种管理
                path: '/layout/currencymanagement',
                name: 'currencyManagement',
                component: () => import( /* webpackChunkName: "popup" */ '../views/currencyManagement/index.vue'),
                meta: {
                    showTitle: true,
                    title: 'title.CurrencyManagement',
                },
            },
            {
                // 上币记录
                path: '/layout/record',
                name: 'record',
                component: () => import( /* webpackChunkName: "popup" */ '../views/record/index.vue'),
                meta: {
                    showTitle: true,
                    title: 'title.Record',
                },
            },
            {
                // 接口文档
                path: '/layout/interface',
                name: 'interface',
                component: () => import( /* webpackChunkName: "popup" */ '../views/interfaceDoc/index2.vue'),
                meta: {
                    showTitle: true,
                    title: 'title.Interface',
                },
            },
            {
                // 图片管理
                path: '/layout/banner',
                name: 'banner',
                component: () => import( /* webpackChunkName: "popup" */ '../views/system/banner/index.vue'),
                meta: {
                    showTitle: true,
                    title: 'title.FriendshipLink',
                },
            },
            {
                // 类别管理
                path: '/layout/category',
                name: 'category',
                component: () => import( /* webpackChunkName: "popup" */ '../views/categoryManagement/index.vue'),
                meta: {
                    showTitle: true,
                    title: 'title.CategoryManagement',
                },
            },
            {
                // 充币列表
                path: '/layout/upCoinList',
                name: 'upCoinList',
                meta: {
                    showTitle: true,
                    title: 'title.RechargeCurrency',
                },
                component: () => import('../views/upCoinList/index.vue'),
            },
            {
                // 提币列表
                path: '/layout/withdrawList',
                name: 'withdrawList',
                meta: {
                    showTitle: true,
                    title: 'title.WithdrawalCurrency',
                },
                component: () => import('../views/withdrawList/index.vue'),
            },
            {
                // 操作日志列表
                path: '/layout/operationLogList',
                name: 'operationLogList',
                meta: {
                    showTitle: true,
                    title: 'title.WithdrawalCurrency',
                },
                component: () => import('../views/operationLogList/index.vue'),
            },

            // {
            //     // 控制室
            //     path: '/layout/dashboard',
            //     name: 'dashboard',
            //     component: () => import( /* webpackChunkName: "popup" */ '../views/dashboard/index.vue'),
            // },
           /* {
                // 我的订单
                path: '/layout/myorder',
                name: 'myorder',
                component: () => import( /!* webpackChunkName: "popup" *!/ '../views/myOrder/index.vue'),
            },*/

        ]
    },
    {
        // 登录页
        path: "/login",
        name: "login",
        components: {
            default: () => import("../views/login/index.vue"),
        },
    },
    {
        // 测试页
        path: "/test",
        name: "test",
        components: {
            default: () => import("../views/test/index.vue"),
        },
    },
];
const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

// 路由守卫
router.beforeEach((to, _, next) => {
    const noToken = ['/login', '/'];
    if (noToken.includes(to.path)) {
        next();
    } else {
        const userStore = useUserStore();
        if (userStore.getToken) {
            next();
        } else {
            next("/login");
        }
    }
});

export default router;
